package com.shop.dao.user;

import com.shop.dao.BaseDao;
import com.shop.pajo.ShoppingCart;
import com.shop.pajo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao{


    @Override
    public User getUserById(Connection connection, int id) throws SQLException {
        PreparedStatement preparedStatement=null;
        ResultSet resultSet = null;
        User user = null;

        if(connection != null) {
            String sql = "select * from user where uid=?";
            Object[] params = {id};

            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql, params);

            while (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("uid"));
                user.setName(resultSet.getString("uname"));
                user.setPassword(resultSet.getString("upassword"));
                user.setEmail(resultSet.getString("email"));
                user.setSalt(resultSet.getString("salt"));
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return user;
    }

    @Override
    public User getUserByName(Connection connection, String name) throws SQLException {
        PreparedStatement preparedStatement=null;
        ResultSet resultSet = null;
        User user = null;

        if(connection != null) {
            String sql = "select * from user where uname=?";
            Object[] params = {name};

            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql, params);

            while (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("uid"));
                user.setName(resultSet.getString("uname"));
                user.setPassword(resultSet.getString("upassword"));
                user.setEmail(resultSet.getString("email"));
                user.setSalt(resultSet.getString("salt"));
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return user;
    }

    @Override
    public User getUserByEmail(Connection connection, String email) throws SQLException {
        PreparedStatement preparedStatement=null;
        ResultSet resultSet = null;
        User user = null;

        if(connection != null) {
            String sql = "select * from user where email=?";
            Object[] params = {email};

            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql, params);

            while (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("uid"));
                user.setName(resultSet.getString("uname"));
                user.setPassword(resultSet.getString("upassword"));
                user.setEmail(resultSet.getString("email"));
                user.setSalt(resultSet.getString("salt"));
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return user;
    }

    @Override
    public int add(Connection connection, User user) throws SQLException {
        PreparedStatement preparedStatement = null;
        int addNum = 0;

        if(connection != null) {
            String sql = "insert into user(uname,upassword,email,salt) " +
                    "values(?,?,?,?)";
            Object[] params = {user.getName(), user.getPassword(), user.getEmail(), user.getSalt()};

            addNum = BaseDao.execute(connection, preparedStatement, sql, params);

            BaseDao.closeResource(null, preparedStatement,null);
        }

        return addNum;
    }

}
